<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
<link rel="icon" href="<c:url value='/imgs/logo.ico' />" />
<link type="text/css" rel="stylesheet" href="<c:url value='/css/index.css' />" />
<link type="text/css" rel="stylesheet" href="<c:url value='/css/add.css' />" />
<script type="text/javascript" charset="utf-8" src="<c:url value='/scripts/jquery-2.1.0.js'/>"></script>
</head>
<body>
	<div class="container">
		<%@ include file="/jsps/top.jsp"%>
		<div class="main">
			<form class="form loginForm">
				<h2>用户登录</h2>
				<span class="loginError">&nbsp;</span> <!-- 显示错误信息 -->
				<table>
					<tr>
						<td><label>工号：</label></td>
						<td><input class="txt" type="text" name="id" placeholder="请输入您的工号" maxlength="8" /></td>
					</tr>
					<tr>
						<td><label>密码：</label></td>
						<td><input class="txt" type="password" name="pw" placeholder="请输入您的密码" maxlength="15"/></td>
					</tr>
				</table>
				<input class="btn loginBtn" type="button" value="登录"/>
				<input class="btn" type="reset" value="重置"/>
			</form>
		</div>
	</div>
	<script type="text/javascript">
		$(function(){
			$(".top .title").click(function(){
				location.href="<c:url value='/index.jsp'/>";
			});	
			
			$(".main .form .loginBtn").click(function(){
				$(".main .form .loginError").text("");
				var id = $("input[name='id']").val();
				var pw = $("input[name='pw']").val();
				if($.isEmptyObject(id.trim())||$.isEmptyObject(pw)){
					alert("工号或密码不能为空！");
				}else{
					$.ajax({
						type : "post",
						url : "<c:url value='/login'/>",
						data : {"id":id,"pw":pw},
						dataType: "json", 
						success : function(data) {
							var path = window.location.href;
							if(data.flag == "1"){
								if(path.lastIndexOf("login.jsp")>0){
									path = "<c:url value='/index.jsp'/>";
								} 
								window.location.href=path;
								window.location.reload;
							}
							else{
								$(".main .form .loginError").text(data.msg);
								//先把错误信息清空，之后把传回的错误信息赋值给msg。
							}
						},
						error : function() {
							alert("操作失败，请重试！");
						}
					});
				}
			});
		})
	</script>
</body>
</html>